﻿using System.Collections.Generic;
using System.Linq.Expressions;
using SilverCrmSoap.CrmSdk;


namespace Linq2Crm.Interpreter
{
	internal partial class ExpressionInterpreter : ExpressionVisitor
	{
		#region Expression Handling Methods
		/// <summary>
		/// </summary>
		/// <param name="c"> </param>
		/// <returns> </returns>
		protected override Expression VisitConstant(ConstantExpression c)
		{
			GetTypeFromConstantExpression(c);

			return base.VisitConstant(c);
		}

		/// <summary>
		/// </summary>
		/// <param name="b"> </param>
		/// <returns> </returns>
		protected override Expression VisitBinary(BinaryExpression b)
		{
			// get operator
			switch (b.NodeType)
			{
				case ExpressionType.AndAlso:
					Visit(b.Left);
					_filters[LogicalOperator.And].Add(_currentCondition);
					_currentCondition = null;
					Visit(b.Right);
					_filters[LogicalOperator.And].Add(_currentCondition);
					_currentCondition = null;
					break;

				case ExpressionType.OrElse:
					Visit(b.Left);
					_filters[LogicalOperator.Or].Add(_currentCondition);
					_currentCondition = null;
					Visit(b.Right);
					_filters[LogicalOperator.Or].Add(_currentCondition);
					_currentCondition = null;
					break;

				default:
					AddFilterCondition(b.NodeType, b.Left, b.Right);
					break;
			}

			return b;
		}

		/// <summary>
		/// </summary>
		/// <param name="m"> </param>
		/// <returns> </returns>
		protected override Expression VisitMethodCall(MethodCallExpression m)
		{
			Expression returnExrepssion = base.VisitMethodCall(m);

			MethodInterpreter(m);

			return returnExrepssion;
		}
		#endregion
	}
}